<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>编辑产品</title>
		<link rel="stylesheet" type="text/css" href="../css/supplyProductsAddStyle.css" />
		<script type="text/javascript" src="../js/jquery.min.js" charset="utf-8"></script>
		<script src="../js/deom.js" type="text/javascript" charset="utf-8"></script>
		<meta name="viewport" content=" height=device-height,
			          width=device-width,
			          initial-scale=1.0,
			          minimum-scale=1.0,
			          maximum-scale=1.0,
			          user-scalable=no" />
		<script type="text/javascript">
			$(function() {
				var ip = localStorage.getItem("ip");
				var proId = localStorage.getItem("proId");
				var userId = localStorage.getItem("userId");
				var token = localStorage.getItem("token");
				var myfiles = localStorage.getItem("myfiles");
				document.getElementById('unitPrice').value = localStorage.getItem("unitPrice");
				document.getElementById('taxPrice').value = localStorage.getItem("taxPrice");
				document.getElementById('proName').value = localStorage.getItem("proName");
				document.getElementById('proType').value = localStorage.getItem("proType");
				document.getElementById('proModel').value = localStorage.getItem("proModel");
				document.getElementById('proSaleNum').value = localStorage.getItem("proSellerNum");
				document.getElementById('proDescribe').value = localStorage.getItem("proDescribe");
				var bomId = localStorage.getItem("sums");
				var bomNum = localStorage.getItem("unms");
				var id = localStorage.getItem("ids");
				var txts = '';
				if(myfiles == null) {
					txts = txts + '<a class="ig">';
					txts = txts + '<img class="imgs" id="imgs" src="http://39.108.187.126:8080/nop.png"/></a>';
				} else {
				    var	arr = myfiles.split(',');
						txts = txts + '<a class="ig">';
						txts = txts + '<img class="imgs" id="imgs" src="' + arr + '"/></a>';
				}
				$(".igclass").append(txts);
				localStorage.removeItem('unitPrice');
				localStorage.removeItem('taxPrice');
				localStorage.removeItem('proName');
				localStorage.removeItem('proType');
				localStorage.removeItem('proModel');
				localStorage.removeItem('proSellerNum');
				localStorage.removeItem('proDescribe');
				localStorage.removeItem('myfiles');
				$('#addbut').click(function() {
					var userId = localStorage.getItem("userId");
					var myfiles = document.getElementById('imgs').src;
					var unitPrice = document.getElementById('unitPrice').value;
					var taxPrice = document.getElementById('taxPrice').value;
					var proName = document.getElementById('proName').value;
					var proType = document.getElementById('proType').value;
					var proModel = document.getElementById('proModel').value;
					var proSaleNum = document.getElementById('proSaleNum').value;
					var proDescribe = document.getElementById('proDescribe').value;
					$.ajax({
						type: 'post', //类型
						url: ip + 'product/modify', //请求路径
						contentType: 'application/x-www-form-urlencoded', //默认
						dataType: 'json', //返回数据类型  可以是text：字符串  可以是json：json对象
						data: {
							'userId': userId,
							'proId': proId,
							'myfiles': myfiles,
							'unitPrice': unitPrice,
							'taxPrice': taxPrice,
							'proName': proName,
							'proType': proType,
							'proModel': proModel,
							'proSaleNum': proSaleNum,
							'proDescribe': proDescribe,
							'bomId': bomId,
							'bomNum': bomNum,
							'token': token,
							'id': id,

						}, //数据
						success: function(result) {
							var myfiles = localStorage.removeItem('myfiles');
							var unitPrice = localStorage.removeItem('unitPrice');
							var taxPrice = localStorage.removeItem('taxPrice');
							var proName = localStorage.removeItem('proName');
							var proType = localStorage.removeItem('proType');
							var proModel = localStorage.removeItem('proModel');
							var proSaleNum = localStorage.removeItem('proSaleNum');
							var proDescribe = localStorage.removeItem('proDescribe');
							var sums = localStorage.removeItem('sums');
							var unms = localStorage.removeItem('unms');
							var ids = localStorage.removeItem('ids');
							alert(result.msg);
							window.location.href = 'supplyProductsList.html';
						},
						error: function(data) { //若果传过来的数据为空
							alert('亲，出错了');
						}
					});
				});
				$('#addbom').click(function() {
					localStorage.setItem("unitPrice", document.getElementById('unitPrice').value);
					localStorage.setItem("myfiles", document.getElementById('unitPrice').src);
					localStorage.setItem("taxPrice", document.getElementById('taxPrice').value);
					localStorage.setItem("proName", document.getElementById('proName').value);
					localStorage.setItem("proType", document.getElementById('proType').value);
					localStorage.setItem("proModel", document.getElementById('proModel').value);
					localStorage.setItem("proSellerNum", document.getElementById('proSaleNum').value);
					localStorage.setItem("proDescribe", document.getElementById('proDescribe').value);
					window.location.href = 'supplyAddProBom.html';
				});

				//展示层
				function showLayer(id) {
					var layer = $('#' + id),
						layerwrap = layer.find('.hw-layer-wrap');
					layer.fadeIn();
					//屏幕居中
					layerwrap.css({
						'margin-top': -layerwrap.outerHeight() / 2
					});
				}

				//隐藏层
				function hideLayer() {
					$('.hw-overlay').fadeOut();
				}

				$('.hwLayer-cancel').on('click', function() {
					hideLayer();
				});
				$('#bot').on('click', function() {
					var formData = new FormData();
					//var name = $("input").val();
					formData.append("myfiles", $("#myfiles")[0].files[0]);
					//formData.append("name", name);
					$.ajax({
						url: ip + 'product/suppeditimage',
						type: 'POST',
						data: formData,
						// 告诉jQuery不要去处理发送的数据
						processData: false,
						// 告诉jQuery不要去设置Content-Type请求头
						contentType: false,
						beforeSend: function() {
							console.log("正在进行,请稍候");
						},
						success: function(result) {
							var re = JSON.parse(result);
							alert(re.data)
							document.getElementById('imgs').src = re.data;
						},
						error: function(result) {
							console.log("error");
						}
					});
					$('.hw-overlay').fadeOut();
				});
				//触发弹出层
				$('.show-layer').on('click', function() {
					var layerid = $(this).data('show-layer');
					showLayer(layerid);
				});

				//点击或者触控弹出层外的半透明遮罩层，关闭弹出层
				$('.hw-overlay').on('click', function(event) {
					if(event.target == this) {
						hideLayer();
					}
				});

				//按ESC键关闭弹出层
				$(document).keyup(function(event) {
					if(event.keyCode == 27) {
						hideLayer();
					}
				});
			});
		</script>
	</head>

	<body>
		<div id="igs" class="igclass">
		</div>
		<textarea class="btn btn-info btn-lg show-layer" data-show-layer="hw-layer" role="button">添加产品图片</textarea>
		<div class="hw-overlay" id="hw-layer">
			<div class="hw-layer-wrap">
				<tr>
					<td><input id="myfiles" style="width: 100%;" name="myfiles" type="file" placeholder="请输选图片" class="easyui-validatebox span3" data-options="required:true" value=""></td>
					<td><button class="btn btn-warning hwLayer-cancel" id="bot">提交</button></td>
				</tr>
				<button class="btn btn-warning hwLayer-cancel">取 消</button>
			</div>
		</div>
		<div class="pro_much">
			<label>价&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;格:</label>
			<input type="text" id="unitPrice" /><br />
			<label>含&nbsp;税&nbsp;价:</label>
			<input type="text" id="taxPrice" />
		</div>
		<div class="pro_info">
			<label>产品名称:</label>
			<input type="text" id="proName" /><br />
			<label>产品类型:</label>
			<input type="text" id="proType" /><br />
			<label>产品型号:</label>
			<input type="text" id="proModel" /><br />
			<label>产品编号:</label>
			<input type="text" id="proSaleNum" /><br />
			<label>产品描述:</label>
			<input type="text" id="proDescribe" /><br />
			<label>产品物料:</label>
			<font>产品物料</font>
			<input id="addbom" type="button" value="   " style="background:url(../img/add.png); width: 8%;" />
		</div>
		<input id="addbut" class="but" type="button" value="保存" />
	</body>

</html>